/**
 * Created by liubin on 2014/07/03.
 */
'use strict';

define(['angular', 'controllers', 'changePwd/changePwdService'], function (angular, controllers) {
    /* Controllers */
    //angular.module('agcompany.controllers', [])
    controllers.controller('changePwdCtrl', ['$scope', '$modal', function ($scope, $modal) {
            // 控制器:更改密码
            console.log('changePwdCtrl');

            // 打开Dialog
            $scope.open = function (size) {
                console.log('open');
                var modalInstance = $modal.open({
                    templateUrl: '/change_pwd',
                    backdrop: 'static',
                    controller: 'changePwdDlgCtrl',
                    size: size
                });
                modalInstance.result.then(
                    function () {
                        console.log('saved');
                    },
                    function () {
                        console.log('cancel');
                    }
                );
            }
        }]
    ).controller('changePwdDlgCtrl', ['$scope', '$modalInstance', 'changePwdService', 'alertService',
            function ($scope, $modalInstance, changePwdService, alertService) {
                console.log('open dialog');
                $scope.alerts = [];
                $scope.save = function (pwd, e) {
                    console.log(e);
                    console.log(pwd);
                    $scope.closeAllAlert();
                    $scope.pwd = pwd;
                    changePwdService.changePwd(
                        {},
                        {
                            old_pwd: pwd.oldPwd,
                            new_pwd: pwd.newPwd
                        },
                        function (success) {
                            console.log(success);
                            if (success.success) {
                                //$window.location.href = "/";
                                $modalInstance.close();
                            }
                            else {
                                console.log(success.messages);
                                angular.forEach(success.messages, function (value, key) {
                                    $scope.alerts.push({type: 'danger', msg: value});
                                });

                            }
                        },
                        // 更新失败的方法
                        function (error) {
                            console.log('error');
                        }
                    );
                };
                $scope.cancel = function () {
                    $modalInstance.dismiss('cancel');
                };
                // 关闭所有错误框
                $scope.closeAlert = function (index) {
                    $scope.alerts.splice(index, 1);
                };

                // 关闭单个错误框
                $scope.closeAllAlert = function () {
                    $scope.alerts.splice(0, $scope.alerts.length);
                };
            }]
    )
});